package com.yunkaweilai.android.view.dialog.room;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.flyco.dialog.utils.CornerUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.room.RoomLableModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

/**
 * 房台标签
 * Created by Administrator on 2016/12/22 0022.
 */

public class GuadanRoomLableDialog extends BaseDialog<GuadanRoomLableDialog> {
    private Context context;
    private ListView idListView;
    private CommonAdapter<RoomLableModel.DataBean.ListBean>adapter;
    private ArrayList<RoomLableModel.DataBean.ListBean>list = new ArrayList<>();
    private OpenRoomDialog dialog;
    private ImageView idImgClose;

    public GuadanRoomLableDialog(Context context,OpenRoomDialog dialog) {
        super(context);
        this.context = context;
        this.dialog = dialog;
    }

    @Override
    public View onCreateView() {
        widthScale(0);
        //showAnim(new Swing());
        //dismissAnim(this, new ZoomOutExit());
        View inflate = View.inflate(mContext, R.layout.dialog_guadan_room_lable, null);
        idListView = ((ListView) inflate.findViewById(R.id.id_listview));
        idImgClose = ((ImageView) inflate.findViewById(R.id.id_img_close));
        //设置背景颜色，和背景的透明圆弧角度
        inflate.setBackgroundDrawable(
                CornerUtils.cornerDrawable(Color.parseColor("#f8f9fa"), dp2px(10)));

        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        initadapter();
        initClick();
        initHttp();
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initClick() {
        idListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RoomLableModel.DataBean.ListBean listBean = list.get(position);
                if ("0".equals(listBean.getStatus()) || "2".equals(listBean.getStatus())){
                    MyToast.showShortToast(mContext,"该标签暂时不能选择");
                }else{
                    dialog.selectedLable(listBean.getLabel_name(),listBean.getId());
                    dismiss();
                }
            }
        });

    }

    private void initadapter() {
        adapter = new CommonAdapter<RoomLableModel.DataBean.ListBean>(mContext,R.layout.item_list_room_lable,list) {
            @Override
            protected void convert(ViewHolder viewHolder, RoomLableModel.DataBean.ListBean item, int position) {
                viewHolder.setText(R.id.id_tv_lable_name,item.getLabel_name() + "");
                TextView idTvStatus = viewHolder.getView(R.id.id_tv_status);
                idTvStatus.setText(Html.fromHtml(item.getStrstatus()));
                if (StringUtils.isEmpty(item.getGroup_title())){
                    viewHolder.setText(R.id.id_tv_room_name, "");
                }else{
                    viewHolder.setText(R.id.id_tv_room_name,item.getGroup_title() + "");
                }
            }
        };
        idListView.setAdapter(adapter);
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.GUADAN_ROOM_LABLE_LIST)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            RoomLableModel roomLableModel = gson.fromJson(json, RoomLableModel.class);
                            list.clear();
                            list.addAll(roomLableModel.getData().getList());
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }
}
